{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# File System and Navigation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import glob\n",
    "import os\n",
    "import shutil\n",
    "import zipfile as zf\n",
    "\n",
    "chapter_name = \"chapter_22\"\n",
    "# Initial cleanup\n",
    "try:\n",
    "    shutil.rmtree(os.path.join(os.getcwd(), chapter_name))\n",
    "except:\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Creating directory\")\n",
    "os.mkdir(chapter_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Changing directory\")\n",
    "os.chdir(chapter_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 3\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Creating a file\")\n",
    "f = open(\"tobedeleted.py\", \"w\")\n",
    "# Some content\n",
    "f.writelines([(str(i) + \"/n\") for i in range(10)])\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 4\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Zipping file\")\n",
    "# Method 1\n",
    "cwd = os.getcwd()\n",
    "shutil.make_archive(\"tobedeleted\", \"zip\", cwd)\n",
    "# Method 2\n",
    "f = open(\"tobedeleted.py\")\n",
    "file_contents = f.read()\n",
    "z = zf.ZipFile(\"tobedeleted-2.zip\", mode=\"w\")\n",
    "z.writestr(\"tobedeleted.py\", file_contents)\n",
    "z.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 5\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Listing directory contents\")\n",
    "print(\"Directory: \" + os.getcwd())\n",
    "files = glob.glob(\"tobedeleted*\")\n",
    "for f in files:\n",
    "    print(f)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 6\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "input(\"Press enter to delete the files and directory...\")\n",
    "for f in files:\n",
    "    # Safety first!\n",
    "    if f.find(\"tobedeleted\") >= 0:\n",
    "        print(f\"Removing {f}\")\n",
    "        os.unlink(f)\n",
    "os.chdir(\"..\")\n",
    "os.rmdir(chapter_name)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
